﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

namespace StorageManage.Logic
{
    public partial class BasicInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method=type.GetMethod(methodName);
            if (method==null)
            {
                throw new Exception("the method is null");
            }
            else
            {
                method.Invoke(this, null);
            }
        }
        public void GetUserInfo()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"]!=null)
            {
                listSearch = ModelTools.GetSearchContions(Request.Form["searchData"]);
                
            }
            Expression<Func<BD_UserInfo, bool>> exp = SearchExpression.FindByGroup<BD_UserInfo>(listSearch);
            var list=DALSession.Current.BD_UserInfoDAL.GetPagedList(OperateContext.Page,OperateContext.Rows,exp,"Id",true);
            int count = DALSession.Current.BD_UserInfoDAL.GetCount(exp);
             Response.Write(JsonHelper.toPageJson(list,count));
        }
        public void EditUser()
        {
            BD_UserInfo userInfo=new BD_UserInfo();
            ModelTools.Form2Model(Request.Form, userInfo);
            userInfo.ManClerk=OperateContext.UserInfo.UserName;
            userInfo.FillDate=DateTime.Now.ToString("yyyy-MM-dd");
            DALSession.Current.BD_UserInfoDAL.ModifyBy(userInfo, o => o.ID == userInfo.ID, "UserName", "UserState", "ManClerk", "FillDate", "Note");
            DALSession.Current.BD_UserInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true));
        }
        public void AddUser()
        {
            BD_UserInfo userInfo = new BD_UserInfo();
            ModelTools.Form2Model(Request.Form, userInfo);
            userInfo.PassWord = "123456";
            userInfo.ManClerk = OperateContext.UserInfo.UserName;
            userInfo.FillDate = DateTime.Now.ToString("yyyy-MM-dd");
            int count = DALSession.Current.BD_UserInfoDAL.GetCount(u => u.UserName == userInfo.UserName);

            if (count > 0)
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "保存失败，已经有同名的用户！"));
            }
            DALSession.Current.BD_UserInfoDAL.Add(userInfo);
            DALSession.Current.BD_UserInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true));
        }
        public void GetTeamUserList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<BD_TeamInfo, bool>> exp = SearchExpression.FindByGroup<BD_TeamInfo>(listSearch);
            string OrderId = OperateContext.Sort == null ? "OrderList" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.BD_TeamInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
            count = DALSession.Current.BD_TeamInfoDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }
        public void EditTeamUser()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            BD_TeamInfo perNew = DALSession.Current.BD_TeamInfoDAL.GetListBy(d => d.Id == ID).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);
            perNew.DepName = DALSession.Current.BD_DepInfoDAL.GetListBy(p => p.ID == perNew.DepId).Select(p => p.DepName).SingleOrDefault();
            perNew.ManClerk = OperateContext.UserInfo.UserName.ToString();
            perNew.FillDate = DateTime.Now.ToString("yyyy-MM-dd");
            DALSession.Current.BD_TeamInfoDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_TeamInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }
        public void AddTeamUser()
        {
            BD_TeamInfo per = new BD_TeamInfo();
            ModelTools.Form2EditModel(Request.Form, per);
            per.DepName = DALSession.Current.BD_DepInfoDAL.GetListBy(p => p.ID == per.DepId).Select(p => p.DepName).SingleOrDefault();
            per.AccountLogIn = Guid.NewGuid().ToString();
            per.ManClerk = OperateContext.UserInfo.UserName.ToString();
            per.FillDate = DateTime.Now.ToString("yyyy-MM-dd");
            per.PassWord = "123456";
            DALSession.Current.BD_TeamInfoDAL.Add(per);
            DALSession.Current.BD_UserInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }
        public void GetWarehouse()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.SM_WarehouseDAL.GetListBy(o => true).Select(o => new { text = o.Warehouse })));  
        }
        public void GetTeam()
        {
            DALSession.Current.BD_DepInfoDAL.GetListBy(o => o.FatherId == 4).Select(o => new {value=o.DepName,text=o.DepName });
        }
        public void GetOutPerson()
        {          
                Response.Write(JsonHelper.toJson( DALSession.Current.BD_TeamInfoDAL.GetListBy(o => true).Select(o => new { text = o.UserName, value = o.UserName})));           
        }
        public void ModifyPwd()
        {
            string oldPwd = Request.Form["oldPwd"];
            string newPwd1 = Request.Form["newPwd1"];
            BD_UserInfo userInfo = OperateContext.UserInfo;
            if (oldPwd != userInfo.PassWord)
	        {
		        Response.Write(JsonHelper.toDefaultMsg(false));
                return;
	        }
            DALSession.Current.BD_UserInfoDAL.ModifyBy(OperateContext.UserInfo, o => userInfo.UserName == o.UserName, "PassWord");
            DALSession.Current.BD_UserInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "修改成功"));

        }
        public void UpdateMaterial()
        {
            string sql = "exec UpdateMaterial";
            var result = SqlHelper.ExecuteScalar(sql);//""
            if (result!=null)
            {
                Response.Write(JsonHelper.toDefaultMsg(true));
            }
            else
            {
                Response.Write(JsonHelper.toDefaultMsg(false));
            }
        }
        public void UpdateTaskCode()
        {
            string sql = "exec UpdateTaskCode";
            var result = SqlHelper.ExecuteScalar(sql);//""
            if (result != null)
            {
                Response.Write(JsonHelper.toDefaultMsg(true));
            }
            else
            {
                Response.Write(JsonHelper.toDefaultMsg(false));
            }
        }
        public void UpdateLinLiao()
        {
            string sql = "exec UpdateLinLiao";
            var result = SqlHelper.ExecuteScalar(sql);//""
            if (result != null)
            {
                Response.Write(JsonHelper.toDefaultMsg(true));
            }
            else
            {
                Response.Write(JsonHelper.toDefaultMsg(false));
            }
        }
    }
}